package com.lgj.web.sys.controller;

import com.github.pagehelper.PageInfo;
import com.lgj.result.ResultBody;
import com.lgj.web.sys.model.SysMenu;
import com.lgj.web.sys.model.SysMenuNode;
import com.lgj.web.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService menuService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody create(@RequestBody SysMenu SysMenu) {
        int count = menuService.create(SysMenu);
        if (count > 0) {
            return ResultBody.success(count);
        } else {
            return ResultBody.fail();
        }
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody update(@PathVariable Long id,
                               @RequestBody SysMenu SysMenu) {
        int count = menuService.update(id, SysMenu);
        if (count > 0) {
            return ResultBody.success(count);
        } else {
            return ResultBody.fail();
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<SysMenu> getItem(@PathVariable Long id) {
        SysMenu SysMenu = menuService.getItem(id);
        return ResultBody.success(SysMenu);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody delete(@PathVariable Long id) {
        int count = menuService.delete(id);
        if (count > 0) {
            return ResultBody.success(count);
        } else {
            return ResultBody.fail();
        }
    }

    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<PageInfo> list(@PathVariable Long parentId,
                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return ResultBody.success(menuService.list(parentId, pageSize, pageNum));
    }

    @RequestMapping(value = "/treeList", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<List<SysMenuNode>> treeList() {
        List<SysMenuNode> list = menuService.treeList();
        return ResultBody.success(list);
    }

    @RequestMapping(value = "/updateHidden/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody updateHidden(@PathVariable Long id, @RequestParam("hidden") Integer hidden) {
        int count = menuService.updateHidden(id, hidden);
        if (count > 0) {
            return ResultBody.success(count);
        } else {
            return ResultBody.fail();
        }
    }
}
